package com.ilongge.rank.Controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ilongge.rank.generator.mapper.CpuMapper;
import com.ilongge.rank.generator.domain.Cpu;

@RestController
public class GetLastestDataController {

    @Autowired
    private CpuMapper cpuMapper;

    @RequestMapping("/app/api/getLastestData")
    public String GetLastestData(HttpServletRequest request) {
        String url = "http://ttt.zxsdata.com:8081/api/cpus";
        String html = HttpUtil.get(url);
        List<Cpu> cpuList = JSON.parseArray(html, Cpu.class);
        for (Cpu cpu : cpuList) {
            cpuMapper.insert(cpu);
        }
        Date currentDate = new Date(); // 获取当前时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd HH:mm:ss");
        String formattedDate = sdf.format(currentDate);
        String remoteAddr = request.getRemoteAddr(); // 获取IP地址
        String retString = "GetLastestData " + formattedDate + " from ip " + remoteAddr;
        System.out.println(retString);
        return retString;
    }
}
